package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.GroupIntroDao;
import com.example.spark.entity.GroupIntro;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.GroupIntroService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

@Service
public class GroupIntroServiceImpl extends ServiceImpl<GroupIntroDao, GroupIntro> implements GroupIntroService {
    @Autowired
    private GroupIntroDao gpDao;
    @Resource
    MinioCloudStorageService minIoService;

    /**
     * 添加小组介绍
     *
     * @param groupIntro 小组介绍
     * @return CommonResult
     */
    @Override
    public CommonResult add(GroupIntro groupIntro) {
        Integer num = gpDao.selectCount(null);
        if (num >= 1) {
            return new CommonResult<>(200, "小组介绍已存在");
        }
        LambdaQueryWrapper<GroupIntro> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GroupIntro::getContent, groupIntro.getContent());
        GroupIntro intro = gpDao.selectOne(lqw);
        if (intro != null) {
            return new CommonResult<>(200, "小组介绍的内容文本已存在");
        }
        lqw.eq(GroupIntro::getPhoto, groupIntro.getPhoto());
        GroupIntro intro2 = gpDao.selectOne(lqw);
        if (intro2 != null) {
            return new CommonResult<>(200, "小组介绍的图片已存在");
        }
        groupIntro.setId(getId());
        groupIntro.setPhoto("000");
        gpDao.insert(groupIntro);
        return new CommonResult<>(200, "success");
    }


    /**
     * 删除小组介绍
     *
     * @param id 小组介绍id
     * @return ComonResult
     */
    @Override
    public CommonResult deleteById(Long id) {
        GroupIntro intro = gpDao.selectById(id);
        if (intro == null) {
            return new CommonResult<>(200, "该小组介绍不存在");
        }
        gpDao.deleteById(id);
        return new CommonResult<>(200, "success");
    }

    /**
     * 更新小组介绍
     *
     * @param groupIntro 小组介绍
     * @return CommonResult
     */
    @Override
    public CommonResult update(GroupIntro groupIntro) {
        GroupIntro intro = gpDao.selectById(groupIntro.getId());
        if (intro == null) {
            return new CommonResult<>(200, "该小组介绍不存在");
        }
        LambdaQueryWrapper<GroupIntro> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GroupIntro::getContent, groupIntro.getContent());
        GroupIntro intro1 = gpDao.selectOne(lqw);
        if (intro1 != null) {
            return new CommonResult<>(200, "小组介绍文本已存在");
        }
        lqw.eq(GroupIntro::getPhoto, groupIntro.getPhoto());
        GroupIntro intro2 = gpDao.selectOne(lqw);
        if (intro2 != null) {
            return new CommonResult<>(200, "小组介绍图片已存在");
        }
        gpDao.updateById(groupIntro);
        return new CommonResult<>(200, "success");
    }

    /**
     * 添加/修改小组介绍图片
     *
     * @param file 图片文件
     * @param id   小组介绍id
     * @return CommonResult
     * @throws IOException
     */

    @Override
    public CommonResult addPicture(MultipartFile file, Long id) throws IOException {
        GroupIntro intro = gpDao.selectById(id);
        if (intro == null) {
            return new CommonResult<>(200, "该小组介绍不存在");
        }
        // getOriginalFileName方法获取的是文件的完整名称，包括文件名称+文件拓展名
        //后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        // 上传文件并获取文件存储路径
        String url = minIoService.uploadPhotoSuffix(file.getBytes(), extension, intro.getId().toString());
        intro.setPhoto(url);
        gpDao.updateById(intro);
        return new CommonResult<>(200, "success");
    }

    @Override
    public CommonResult get() {
        GroupIntro groupIntro = gpDao.selectOne(null);
        return new CommonResult<>(200, "success", groupIntro);
    }
}
